import { createRouter, createWebHashHistory  } from 'vue-router'
import { ref } from 'vue'
import HomeView from '../views/home/index.vue'
import DownloadManage from '../views/download-manage/index.vue'
import DownloadFinish from '../views/download-finish/index.vue'
import UserOverLoad from '../views/user-over-load/index.vue'
import { useConfigData } from '@/stores/config-data'
import {useUserInfo} from '@/stores/user-info'
import { storeToRefs } from 'pinia'
import { invokeAsyncAPi } from '@/utils/xz-fn'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/downloadManage',
      name: 'downloadManage',
      component: DownloadManage
    },
    {
      path: '/downloadFinish',
      name: 'downloadFinish',
      component: DownloadFinish
    },
    {
      path: '/userOverLoad',
      name: 'userOverLoad',
      component: UserOverLoad
    },
  ]
})

router.beforeEach(async (to, from, next) => {
  const configDataStore = useConfigData();
  const userInfoStore = useUserInfo();
  const { configData } = storeToRefs(configDataStore);
  if (!configData.value.app_id) {//第一次，获取appid
    const _configData = await invokeAsyncAPi('fq_GetClientInfo')
    configDataStore.setConfigData(_configData);
  }
  const _isLogin = await invokeAsyncAPi('fq_IsLogin')
  userInfoStore.setIsLogin(_isLogin);
  const _isMember = await invokeAsyncAPi('fq_IsVip')
  userInfoStore.setIsMember(_isMember);
  const _headImg = await invokeAsyncAPi('fq_GetHeadImgPath')
  userInfoStore.setHeadImg(_headImg);
  const _nickName = await invokeAsyncAPi('fq_GetLoginUserName')
  userInfoStore.setNickName(_nickName);
  const { lift_member, expired_time } = await invokeAsyncAPi('fq_GetVipTimeStatus')
  userInfoStore.setIsNoLimitMember(lift_member)
  userInfoStore.setMemberExpireTime(expired_time)
  const _token = await invokeAsyncAPi('fq_GetLoginToken')
  userInfoStore.setToken(_token);
  next()
})


export default router
